/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.controllers.platformer
{
	import com.puny.mvc.controllers.Controller;
	import com.puny.mvc.helpers.FrameHelper;
	
	import flash.geom.Point;

	/**
	 * $Revision: 845 $
	 */
	public class EasingController extends Controller
	{
		private var _target:Point = new Point();
		private var _loop:Boolean = false;
		private var _rate:Number = .3;
		private var _modulus:Number = _rate;
		private var _activeX:Boolean = false;
		private var _activeY:Boolean = false;
		private var _v:Number = 0;

		public function EasingController(filename:String=null)
		{
			super(filename);
		}
		
		public function set rate(value:Number):void {
			_rate = value;
		}
		
		public function jumpTo(dx:Number,dy:Number):void {
			_modulus = _rate;
			moveTo(dx,dy);
		}
		public function slideTo(dx:Number,dy:Number,lazy:Boolean=false):void {
			_modulus = _rate;
			moveTo(dx,dy,lazy);
		}
		
		private function moveTo(dx:Number,dy:Number,lazy:Boolean=false):void {
			x += dx;
			y += dy;
//			if (lazy) {
//				dx = (dx > 100 || dx < -100)? dx : 0;
////				dy = (dy > 100 || dy < -100)? dy : 0;
//			}
//			if (dx == 0 && dy == 0) {
//				return;
//			}
//			
//			_target.x = x + dx;
//			_target.y = y + dy;
//
//			_activeX = (dx != 0);
//			_activeY = (dy != 0);
//			//TODO: It should be synchronized with main character's speed.
//			_v = 100;
//			if (!_loop) {
//				_loop = true;
//				FrameHelper.setup(this,startSliding,slideHandler,slideEndHandler);
//			}
		}

		private var _upper:Number;
		private function startSliding():void {
			_upper = 0;
		}
		
		private function upperLimit(d:Number):Number {
			var v:Number = _upper * _v;
			_upper = _upper < 1 ? (_upper +.1) : 1;
			return d > v ? v : (d < -v) ? -v : d; 
		}
		
		private function slideHandler():Boolean {
			var tx:Number =_target.x - x;
			var ty:Number =_target.y - y;
			if (ty > stage.stageHeight) {
				y = _target.y + stage.stageHeight * .5;
				return true;
			} else if ( ty < - stage.stageHeight) {
				y = _target.y - stage.stageHeight * .5;
				return true;
			} 
			var dx:Number = upperLimit(tx)*_modulus;
			var dy:Number = upperLimit(ty)*_modulus;
//			trace("dx=",dx,":dy=",dy,tx,ty);
			if (_activeX) {
				x += _v < dx ? _v : dx;
			}
			if (_activeY) {
				y += _v < dy ? _v : dy;
			}
//			_v /= _rate;
			return (tx * tx) > 1 || (ty * ty) > 1;
		}
		
		private function slideEndHandler():void {
			_loop = false;
			if (_activeX) {
				x = _target.x;
			}
			if (_activeY) {
				y = _target.y;
			}
		}
		
	}
}